﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IRepository;
using Models;
using Infrastructure;
using System.Data;
using Spring.Data.Common;
using Spring.Data.Generic;

namespace RepositoryMSSQL
{
    public class ExecuteRecordRepository : IExecuteRecordRepository
    {
        private static string strBase = "select Code, Function_Name, CostTime, Action_From, Create_Date"
            + " from Execute_Record ";

        public void CreateRecord(ExecuteRecord record)
        {
            string strSql = "insert into Execute_Record(Code, Function_Name, CostTime, Action_From, Create_Date) "
                + " values(@Code, @Function_Name, @CostTime, @Action_From, @Create_Date) ";

            string strSearch = "SELECT isnull(max(Code),'R000000000') FROM Execute_Record ";
            string strMaxNo = DBHelper.SpringDBTemplate.ExecuteScalar(CommandType.Text, strSearch).ToString(); 
            int nNum = Int32.Parse(strMaxNo.Substring(1)) + 1;
            record.Code = "R" + nNum.ToString("000000000");

            IDbParameters para = DBHelper.SpringDBTemplate.CreateDbParameters();
            para.Add("Code", DbType.String).Value = record.Code;
            para.Add("Function_Name", DbType.String).Value = record.FunctionName;
            para.Add("CostTime", DbType.String).Value = record.CostTime;
            para.Add("Action_From", DbType.String).Value = record.ActionFrom;
            para.Add("Create_Date", DbType.DateTime).Value = record.CreateTime;

            DBHelper.SpringDBTemplate.ExecuteScalar(CommandType.Text, strSql, para);
        }

        public ExecuteRecord GetRecord(string code)
        {
            throw new NotImplementedException();
        }

        public List<ExecuteRecord> ListRecord()
        {
            return DBHelper.SpringDBTemplate.QueryWithRowMapperDelegate(CommandType.Text, strBase,
                new RowMapperDelegate<ExecuteRecord>(MapRow)).ToList();
        }

        public List<ExecuteRecord> ListRecord(DateTime startTime, DateTime endTime)
        {
            string strSql = strBase + " where create_date > @startTime and create_date< @endTime ";

            IDbParameters para = DBHelper.SpringDBTemplate.CreateDbParameters();
            para.Add("startTime", DbType.DateTime).Value = startTime;
            para.Add("endTime", DbType.DateTime).Value = endTime;

            return DBHelper.SpringDBTemplate.QueryWithRowMapperDelegate(CommandType.Text, strSql,
                new RowMapperDelegate<ExecuteRecord>(MapRow), para).ToList();
        }

        public List<ExecuteRecord> ListRecord(string functionName)
        {
            throw new NotImplementedException();
        }

        public List<ExecuteRecord> ListRecord(double costTime)
        {
            throw new NotImplementedException();
        }

        public List<ExecuteRecord> ListRecordByAction(string actionFrom)
        {
            throw new NotImplementedException();
        }

        #region Helper Methods
        private static ExecuteRecord MapRow(IDataReader dr, int count)
        {
            ExecuteRecord obj = new ExecuteRecord();

            obj.Code = dr.IsDBNull(0) ? String.Empty : dr.GetString(0);
            obj.FunctionName = dr.IsDBNull(1) ? String.Empty : dr.GetString(1);
            obj.CostTime = (double)dr.GetDouble(2);
            obj.ActionFrom = dr.IsDBNull(3) ? String.Empty : dr.GetString(3);
            obj.CreateTime = dr.IsDBNull(4) ? DateTime.MinValue : dr.GetDateTime(4);

            return obj;
        }
        #endregion
    }
}
